#!/usr/bin/env bash

# Author:   Zhang Huangbin (zhb _at_ iredmail.org)

#---------------------------------------------------------------------
# This file is part of iRedMail, which is an open source mail server
# solution for Red Hat(R) Enterprise Linux, CentOS, Debian and Ubuntu.
#
# iRedMail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iRedMail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iRedMail.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------

# Warning: Please source 'conf/postfix' first.

export SYS_USER_AMAVISD='amavis'
export SYS_GROUP_AMAVISD='amavis'
export AMAVISD_QUARANTINE_PORT='9998'
export AMAVISD_LOGFILE='/var/log/amavisd.log'
export AMAVISD_LOGROTATE_FILE="${LOGROTATE_DIR}/amavisd"
export AMAVISD_DKIM_SELECTOR='dkim'
export AMAVISD_RC_SCRIPT_NAME='amavisd'

export AMAVISD_SPOOL_DIR='/var/spool/amavisd'
export AMAVISD_DKIM_DIR="${AMAVISD_DKIM_DIR:=/var/lib/dkim}"

# Network ports
export AMAVISD_SMTP_PORT='10024'
export AMAVISD_ORIGINATING_PORT='10026'
export AMAVISD_MLMMJ_PORT='10027'
export AMAVISD_QUARANTINE_PORT='9998'

# Database used to store incoming & outgoing email related information.
export AMAVISD_DB_NAME='amavisd'
export AMAVISD_DB_USER='amavisd'

# Altermime.
export CMD_ALTERMIME='/usr/bin/altermime'
export DISCLAIMER_DIR="${POSTFIX_ROOTDIR}/disclaimer"

if [ X"${DISTRO}" == X'RHEL' ]; then
    export AMAVISD_BIN='/usr/sbin/amavisd'
    export AMAVISD_LDAP_SCHEMA_NAME='amavisd-new.schema'
    export AMAVISD_VAR_RUN_DIR='/var/run/amavisd'

    export AMAVISD_CONF='/etc/amavisd/amavisd.conf'

elif [ X"${DISTRO}" == X'DEBIAN' -o X"${DISTRO}" == X'UBUNTU' ]; then
    export AMAVISD_BIN='/usr/sbin/amavisd-new'
    export AMAVISD_CONF_DIR='/etc/amavis/conf.d'
    export AMAVISD_CONF='/etc/amavis/conf.d/50-user'
    export AMAVISD_RC_SCRIPT_NAME='amavis'
    export AMAVISD_LDAP_SCHEMA_NAME='amavis.schema'

    export AMAVISD_SPOOL_DIR='/var/lib/amavis'
    export AMAVISD_VAR_RUN_DIR='/var/run/amavis'

elif [ X"${DISTRO}" == X'FREEBSD' ]; then
    # Override global setting: user/group.
    export SYS_USER_AMAVISD='vscan'
    export SYS_GROUP_AMAVISD='vscan'

    export AMAVISD_BIN='/usr/local/sbin/amavisd'
    export AMAVISD_CONF='/usr/local/etc/amavisd.conf'
    export AMAVISD_LDAP_SCHEMA_NAME='amavisd-new.schema'

    export AMAVISD_SPOOL_DIR='/var/amavis'
    export AMAVISD_VAR_RUN_DIR="${AMAVISD_SPOOL_DIR}/var"

    # Override location of command 'altermime'
    export CMD_ALTERMIME='/usr/local/bin/altermime'

elif [ X"${DISTRO}" == X'OPENBSD' ]; then
    export SYS_USER_AMAVISD='_vscan'
    export SYS_GROUP_AMAVISD='_vscan'

    export AMAVISD_BIN='/usr/local/sbin/amavisd'
    export AMAVISD_CONF='/etc/amavisd.conf'
    export AMAVISD_LDAP_SCHEMA_NAME='amavisd.schema'

    export AMAVISD_SPOOL_DIR='/var/amavisd'
    export AMAVISD_VAR_RUN_DIR="${AMAVISD_SPOOL_DIR}"

    # Override location of command 'altermime'
    export CMD_ALTERMIME='/usr/local/bin/altermime'

fi

export CRON_FILE_AMAVISD="${CRON_SPOOL_DIR}/${SYS_USER_AMAVISD}"

export AMAVISD_DB_DIR="${AMAVISD_SPOOL_DIR}/db"
export AMAVISD_QUARANTINE_DIR="${AMAVISD_SPOOL_DIR}/quarantine"
export AMAVISD_TEMP_DIR="${AMAVISD_SPOOL_DIR}/tmp"

if [ X"${DISTRO}" == X'RHEL' ]; then
    export AMAVISD_VIRUSMAILS_DIR="${AMAVISD_QUARANTINE_DIR}"
elif [ X"${DISTRO}" == X'DEBIAN' -o X"${DISTRO}" == X'UBUNTU' ]; then
    export AMAVISD_VIRUSMAILS_DIR="${AMAVISD_SPOOL_DIR}/virusmails"
elif [ X"${DISTRO}" == X'FREEBSD' ]; then
    export AMAVISD_VIRUSMAILS_DIR='/var/virusmails'
elif [ X"${DISTRO}" == X'OPENBSD' ]; then
    export AMAVISD_VIRUSMAILS_DIR='/var/virusmails'
fi

export AMAVISD_LOCK_FILE="${AMAVISD_VAR_RUN_DIR}/amavisd.lock"
export AMAVISD_PID_FILE="${AMAVISD_VAR_RUN_DIR}/amavisd.pid"
export AMAVISD_SOCKET_FILE="${AMAVISD_VAR_RUN_DIR}/amavisd.socket"

export AMAVISD_DB_MYSQL_TMPL="${SAMPLE_DIR}/amavisd/amavisd.mysql"
export AMAVISD_DB_PGSQL_TMPL="${SAMPLE_DIR}/amavisd/amavisd.pgsql"

# Value of Postfix parameter `content_filter=`.
export AMAVISD_CONTENT_FILTER_ORIGINATING="smtp-amavis:[${SMTP_SERVER}]:${AMAVISD_ORIGINATING_PORT}"

# mynetworks, inet_acl
export AMAVISD_MYNETWORKS='127.0.0.0/8'
export AMAVISD_INET_ACL='127.0.0.1 [::1]'
if [ X"${LOCAL_ADDRESS}" != X'127.0.0.1' ]; then
    export AMAVISD_MYNETWORKS="${AMAVISD_MYNETWORKS},${LOCAL_ADDRESS}"
    export AMAVISD_INET_ACL="${AMAVISD_INET_ACL} ${LOCAL_ADDRESS}"
fi

# Num of pre-forked children.
# Try to set it to memory size (in GB)
export AMAVISD_MAX_SERVERS='2'
if [ X"${KERNEL_NAME}" == X'LINUX' ]; then
    _mem_size_gb="$(free -g | grep '^Mem:' | awk '{print $2}')"
    export AMAVISD_MAX_SERVERS="$((${_mem_size_gb} + 1))"
elif [ X"${KERNEL_NAME}" == X'FREEBSD' ]; then
    _mem_size="$(sysctl hw.realmem | awk '{print $2}')"
    export AMAVISD_MAX_SERVERS="$((${_mem_size} / 1024 / 1024 / 1024 + 1))"
elif [ X"${KERNEL_NAME}" == X'OPENBSD' ]; then
    _mem_size="$(sysctl hw.physmem | awk -F'=' '{print $2}')"
    export AMAVISD_MAX_SERVERS="$((${_mem_size} / 1024 / 1024 / 1024 + 1))"
fi
